package com.qf.fmall2202.shopcart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.fmall2202.product.entity.Product;
import com.qf.fmall2202.product.entity.ProductImg;
import com.qf.fmall2202.product.entity.ProductSku;
import com.qf.fmall2202.product.service.IProductImgService;
import com.qf.fmall2202.product.service.IProductService;
import com.qf.fmall2202.product.service.IProductSkuService;
import com.qf.fmall2202.shopcart.entity.ShoppingCart;
import com.qf.fmall2202.shopcart.mapper.ShoppingCartMapper;
import com.qf.fmall2202.shopcart.service.IShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 购物车  服务实现类
 * </p>
 *
 * @author dong_
 * @since 2022-11-08
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements IShoppingCartService {

    @Autowired
    IProductService productService;

    @Autowired
    IProductImgService productImgService;

    @Autowired
    IProductSkuService productSkuService;


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<ShoppingCart> listbycids(String cids) {

        //1. cids 按逗号分隔进行切分
        final String[] cidArray = cids.split(",");

        //2. 查询 cidArray 对应的 购物车中的 信息
        final List<ShoppingCart> shoppingCarts = this.list(new QueryWrapper<ShoppingCart>().
                        in("cart_id", Arrays.asList(cidArray)));

        //3. 填充缺失 的信息
        populateCart(shoppingCarts);

        return shoppingCarts;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<ShoppingCart> listByUserId(Integer userId) {

        //1. 查询当前用户购物车信息
        final List<ShoppingCart> shopcartList = this.list(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getUserId, userId));

        //2. 补全 ShoppingCart 对象中缺少的信息
        populateCart(shopcartList);

        return shopcartList;
    }


    public void populateCart(List<ShoppingCart> shopcartList) {

        //1. 查商品表
        // List<ShoppingCart>  ---> List<商品id>
        final List<String> productIdList = shopcartList
                .stream().
                        map(ShoppingCart::getProductId).collect(Collectors.toList());
        final Map<String, String> productIdNameMap = productService.list(new QueryWrapper<Product>()
                .in("product_id", productIdList))
                .stream()
                .collect(Collectors.toMap(Product::getProductId, Product::getProductName));

        //2. 查商品图片表
        final Map<String, String> productIdImgMap = productImgService.list(new QueryWrapper<ProductImg>()
                .eq("is_main", 1)
                .in("item_id", productIdList))
                .stream()
                .collect(Collectors.toMap(ProductImg::getItemId, ProductImg::getUrl));

        //3. 查sku表
        // List<ShoppingCart>  ---> List<SkuId>
        final List<String> skuIdList = shopcartList.stream().map(ShoppingCart::getSkuId).collect(Collectors.toList());
        final Map<String, ProductSku> skuIdValueMap = productSkuService.list(new QueryWrapper<ProductSku>().in("sku_id", skuIdList))
                .stream()
                .collect(Collectors.toMap(ProductSku::getSkuId, productSku -> productSku));


        shopcartList.forEach(shoppingCart -> {

            shoppingCart.setProductName(productIdNameMap.get(shoppingCart.getProductId()));

            shoppingCart.setProductImg(productIdImgMap.get(shoppingCart.getProductId()));

            /**
             *  "skuName": "咪咪虾条 单包 50g",
             *             "originalPrice": 2,
             *             "sellPrice": 1,
             *             "stock": 277
             */
            final ProductSku productSku = skuIdValueMap.get(shoppingCart.getSkuId());
            shoppingCart.setSkuName(productSku.getSkuName());
            shoppingCart.setOriginalPrice(productSku.getOriginalPrice());
            shoppingCart.setSellPrice(productSku.getSellPrice());
            shoppingCart.setStock(productSku.getStock());

        });

    }
}
